@file:OptIn(ExperimentalFoundationApi::class)

package com.wonderful.eyepetizer

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.PagerState
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.NavigationBarItemDefaults
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.wonderful.eyepetizer.ui.pages.daily.DailyPage
import com.wonderful.eyepetizer.ui.pages.discover.DiscoverPage
import com.wonderful.eyepetizer.ui.pages.hot.HotPage
import com.wonderful.eyepetizer.ui.pages.person.PersonPage
import com.wonderful.eyepetizer.ui.theme.EyepetizerComposeTheme
import com.wonderful.eyepetizer.ui.theme.NormalItemColor
import com.wonderful.eyepetizer.ui.theme.SelectItemColor
import com.wonderful.eyepetizer.view.FullScreenScaffold
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            val pagerState = rememberPagerState(pageCount = { 4 })

            EyepetizerComposeTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    FullScreenScaffold(bottomBar = {
                        BottomNavigationBar(pagerState = pagerState)
                    }) {
                        ContentScreen(padding = it, pagerState = pagerState)
                    }
                }
            }
        }
    }
}


@Composable
fun ContentScreen(padding: PaddingValues, pagerState: PagerState) {
    HorizontalPager(
        state = pagerState,
        userScrollEnabled = false,
        modifier = Modifier
            .fillMaxSize()
            .background(Color.White)
            .padding(padding)
    ) { index ->
        when (index) {
            0 -> DailyPage()
            1 -> DiscoverPage()
            2 -> HotPage()
            3 -> PersonPage()
        }
    }
}

@Composable
fun BottomNavigationBar(pagerState: PagerState) {
    val viewModel: MainViewModel = viewModel()
    val scope = rememberCoroutineScope()

    // 这边的 containerColor 需要和 子项 NavigationBarItem 的 colors 里的 indicatorColor 设置成相同颜色
    NavigationBar(containerColor = Color.White) {
        viewModel.tabs.value.forEachIndexed { index, tabItem ->
            NavigationBarItem(
                selected = viewModel.selectedTabIndex.intValue == index,
                onClick = {
                    viewModel.selectedTabIndex.intValue = index
                    scope.launch { pagerState.scrollToPage(index) }
                },
                label = {
                    Text(
                        text = stringResource(id = tabItem.title),
                        color = if (viewModel.selectedTabIndex.intValue == index) SelectItemColor else NormalItemColor
                    )
                },
                icon = {
                    Icon(
                        painter = painterResource(id = if (viewModel.selectedTabIndex.intValue == index) tabItem.selectIcon else tabItem.normalIcon),
                        contentDescription = "",
                        modifier = Modifier.size(24.dp),
                    )
                },
                colors = NavigationBarItemDefaults.colors(indicatorColor = Color.White)
            )
        }
    }
}

data class TabItem(
    @StringRes val title: Int,
    @DrawableRes val normalIcon: Int,
    @DrawableRes val selectIcon: Int
)